package com.neusoft.liduan.chess;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.io.File;

import javax.swing.JPanel;

public class Chess {
	private String name;//名称
	private int x;//x的坐标位置，指定图片绘制位置
	private int y;//y的坐标位置，指定图片绘制位置
	private Point p;//坐标值，用于计算x和y
	private static final int size = 30;//大小
	private static final int space = 40;//间隔
	private static final int margin = 20;//边距
	private int player;//阵营
	//File.separator：生成路径分隔符，在win为\，在linux为/
	private String folder = "pic" + File.separator;
	private String suffix = ".png";
	
	public void draw(Graphics g, JPanel panel) {
		Image img = Toolkit.getDefaultToolkit().getImage(folder + name + player + suffix);
		g.drawImage(img, x, y, size, size, panel);
	}
	
	public Chess() {
		
	}
	
	public Chess(String name, Point p, int player) {
		this.name = name;
		setP(p);
		this.player = player;
	}
	
	public void calXY() {
		x = margin - size / 2 + space * (p.x - 1);
		y = margin - size / 2 + space * (p.y - 1);
	}
	
	/**
	 * 	根据点击的坐标xy生成Point坐标
	 * @param x
	 * @param y
	 * @return
	 */
	public static Point getPointFromXY(int x, int y) {
		Point p = new Point();
		p.x = (x - margin + size / 2) / space + 1;
		p.y = (y - margin + size / 2) / space + 1;
		
		return p;
	}
	
	/**
	 * 反转坐标
	 */
	public void reserve() {
		p.x = 10 - p.x;
		p.y = 11 - p.y;
		setP(p);
	}
	
	//在类中属性一般是不允许直接在其它类中直接使用的，建议使用get和set方法来设置获取属性值
	public void setName(String name) {
		this.name = name;
	}
	
	public String getName() {
		return this.name;
	}
	
	public void setP(Point p) {
		this.p = p;
		calXY();
	}
	
	public Point getP() {
		return this.p;
	}
	
	public void setPlayer(int player) {
		this.player = player;
	}
	
	/*
	 * toString()方法是属于Object类的
	 * 	由子类重写
	 */
	@Override
	public String toString() {
		//super.toString()：调用父类中toString()方法
//		return super.toString();
		return "Chess [name=" + name + ", x=" + x + ", y=" + y + ", p=" + p + ", size=" + size + ", space=" + space
				+ ", margin=" + margin + ", player=" + player + ", folder=" + folder + ", suffix=" + suffix + "]";
	}
	
	
}
